package kr.ac.kaist.ysproject;

import java.util.ArrayList;

import kr.ac.kaist.ysproject.data.DataMsgHistory;
import kr.ac.kaist.ysproject.data.DataProfile;
import kr.ac.kaist.ysproject.util.DbManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class MainListAdapter extends ArrayAdapter<DataProfile> {
	
	private Context mContext;
	private int mResource;
	private ArrayList<DataProfile> mList;
	private LayoutInflater mInflater;
	
	public MainListAdapter(Context context, int layoutResource, ArrayList<DataProfile> objects) {
		
		super(context, layoutResource, objects);
		this.mContext = context;
		this.mResource = layoutResource;
		this.mList = objects;
		this.mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		DataProfile dataProfile = mList.get(position);

		if(convertView == null) {
			
			convertView = mInflater.inflate(mResource, null);
		}

		if(dataProfile != null) {
		
			// 이미지
			String picturePath = dataProfile.getPicturePath();
        	if (!picturePath.equals("")) {
        		Bitmap bMap = BitmapFactory.decodeFile(picturePath);
                ImageView image = (ImageView) convertView.findViewById(R.id.imageViewPersonPic);
                image.setImageBitmap(bMap);
        	}
			
			// 이름
			((TextView)convertView.findViewById(R.id.textViewPersonName)).setText(dataProfile.getName());
			
			// 최신 메시지 내용
			DbManager dbm = DbManager.getInstance(convertView.getContext());
			ArrayList<DataMsgHistory> msgInfo = dbm.getMsgHistory(Integer.toString(dataProfile.getId()));

			if (msgInfo.size() > 0) {
				((TextView)convertView.findViewById(R.id.textViewPersonRM)).setText(msgInfo.get(0).getMsg());
			} else {
				((TextView)convertView.findViewById(R.id.textViewPersonRM)).setText("최근 메시지 없음.");
			}
			

			// 스마트메시지 이미지뷰/사진 이미지뷰 이외 부분 클릭 => 문자 히스토리 리스트 화면을 띄워준다.
			final int pos = position;
			convertView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(v.getContext(), MsgHistoryActivity.class);
					intent.putExtra("ProfileId", Integer.toString(mList.get(pos).getId()));
					intent.putExtra("ProfileName", mList.get(pos).getName());
					intent.putExtra("PicturePath", mList.get(pos).getPicturePath());
					
					v.getContext().startActivity(intent);
				}
			});
			
			// 스마트메시지 이미지뷰 클릭 => 스마트메시지 리스트 화면을 띄워준다.
			((ImageButton)convertView.findViewById(R.id.imageButtonPersonSM)).setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					
					Intent intent = new Intent(v.getContext(), SmartMessageListActivity.class);
					intent.putExtra("ProfileId", Integer.toString(mList.get(pos).getId()));
					intent.putExtra("ProfileName", mList.get(pos).getName());
					intent.putExtra("PicturePath", mList.get(pos).getPicturePath());
					
					v.getContext().startActivity(intent);
				}
			});
			
			// 사진이미지 부분 클릭 => 프로파일 편집 화면을 띄워준다.
			((ImageView)convertView.findViewById(R.id.imageViewPersonPic)).setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(v.getContext(), ProfileActivity.class);
					intent.putExtra("ObjectData", mList.get(pos));
					v.getContext().startActivity(intent);	
				}
			});
			
		}
		return convertView;
	}
}
